package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MenuDao;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : [chenxys]
 * @version : [v1.0]
 * @Description : 菜单服务接口实现
 * @createTime : [2022/6/2 18:27]
 */
@Service(interfaceClass = MenuService.class)
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;


    /**
     * 根据角色ID查询对应的菜单
     * @author chenxys
     * @param roleId 角色ID
     * @return java.util.List<java.util.Map>
     */
    @Override
    public List<Map> getMenuByUserRoleId(Integer roleId) {
        //存放最后返回的数据
        List<Map> data = new ArrayList<Map>();
        //获取当前角色所拥有的一级菜单
        List<Menu> firstLevelMenu = menuDao.findFirstLevelMenu(roleId);
        if (firstLevelMenu != null && firstLevelMenu.size() > 0){
            //一级菜单
            Map<String,Object> firstMenu = null;
            //二级菜单
            Map<String,Object> secondMenu = null;
            //存放二级菜单
            List<Map> secondList = null;
            //获取每个一级菜单所拥有的二级菜单
            for (Menu menu : firstLevelMenu) {
                firstMenu = new HashMap<>();
                //path
                firstMenu.put("path",menu.getPath());
                //title
                firstMenu.put("title",menu.getName());
                //icon
                firstMenu.put("icon",menu.getIcon());
                //children
                List<Menu> secondLevelMenu = menuDao.findSecondLevelMenu(roleId, menu.getId());
                //获取每个一级菜单所拥有的二级菜单
                secondList = new ArrayList<>();
                for (Menu second : secondLevelMenu) {
                    secondMenu = new HashMap<>();
                    //path
                    secondMenu.put("path",second.getPath());
                    //title
                    secondMenu.put("title",second.getName());
                    //icon
                    secondMenu.put("icon",second.getIcon());
                    //linkUrl
                    secondMenu.put("linkUrl",second.getLinkUrl());
                    //加入list集合
                    secondList.add(secondMenu);
                }
                firstMenu.put("children",secondList);
                data.add(firstMenu);
            }
            return data;
        }else {
            return null;
        }
    }
}
